package com.dmgis.qxfw.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerialUtil {

	//序列化
	public String serializeToString(Object obj) throws IOException{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(obj);
		String str = bos.toString("ISO-8859-1");//此处只能是ISO-8859-1,但是不会影响中文使用
		bos.close();
		oos.close();
		return str;
	}
	
	//反序列化
	public Object deserializeToObject(String str) throws IOException, ClassNotFoundException{
		ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
		ObjectInputStream ois = new ObjectInputStream(bis);
		Object obj = ois.readObject();
		bis.close();
		ois.close();
		return obj;
		
	}
}
